package com.ceo.common.http.response;

import com.ceo.common.i18.util.LanguageTransUtil;
import com.ceo.common.protocol.PlatformErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @author uto_dev_008
 * @desc
 * @since 2021/11/22 15:56
 * Copyright (c)  All rights reserved
 */
public class DefaultResponse<T> extends AbstractResponse implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(DefaultResponse.class);

    private static final long serialVersionUID = 5839617352026682609L;


    public DefaultResponse() {
        super.setCode(PlatformErrorCode.SYS_OK.getCode());
        super.setMsg("success");
        this.data = null;
    }

    public DefaultResponse(int errorCode, String message) {
        super.setCode(errorCode);
        super.setMsg(message);
        this.data = null;
    }

    public DefaultResponse(T data, int errorCode, String message) {
        super.setCode(errorCode);
        super.setMsg(message);
        this.data = data;
    }

    public DefaultResponse(T data, int success, int errorCode, String message) {
        super.setCode(errorCode);
        super.setMsg(message);
        this.data = data;
    }

    public DefaultResponse(T data) {
        super.setCode(PlatformErrorCode.SYS_OK.getCode());
        super.setMsg("success");
        this.data = data;
    }

    /*public static DefaultResponse success(Object data){
        return new DefaultResponse(data);
    }*/

    public static <T> DefaultResponse<T> success(T data) {
        return new DefaultResponse(data);
    }

    public static <T> DefaultResponse<T> success() {
        return new DefaultResponse(null);
    }

    public static DefaultResponse error(PlatformErrorCode errorCode) {
        String tmp = LanguageTransUtil.getMessage(String.valueOf(errorCode.getCode()));
        return new DefaultResponse(errorCode.getCode(), tmp);
    }

    public static DefaultResponse error(int errorCode) {
        String tmp = LanguageTransUtil.getMessage(String.valueOf(errorCode));
        return new DefaultResponse(errorCode, tmp);
    }

    public static DefaultResponse error(int errorCode, Object data) {
        return new DefaultResponse(data, errorCode, LanguageTransUtil.getMessage(String.valueOf(errorCode)));
    }

    public static DefaultResponse error(int errorCode, String message, Object data) {
        return new DefaultResponse(data, errorCode, String.format(LanguageTransUtil.getMessage(String.valueOf(errorCode)), message));
    }

    public static DefaultResponse error(int errorCode, String message, Object data, boolean i18) {
        String lastMessage = i18 ? String.format(LanguageTransUtil.getMessage(String.valueOf(errorCode)), message) : message;
        return new DefaultResponse(data, errorCode, lastMessage);
    }

    public static DefaultResponse error(int errorCode, String message) {
        return new DefaultResponse(errorCode, String.format(LanguageTransUtil.getMessage(String.valueOf(errorCode)), message));
    }

    public static DefaultResponse error(int errorCode, String message, boolean i18) {
        return new DefaultResponse(errorCode, i18 ? String.format(LanguageTransUtil.getMessage(String.valueOf(errorCode)), message) : message);
    }

    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
